package tabdulin.sms.visual;

import java.io.IOException;
import java.util.Properties;

import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;

import tabdulin.sms.contacts.ContactList;

import tabdulin.sms.contacts.ContactList;

/**
 * @author Talgat Abdulin
 *
 */
public class GUI {
    private final static Logger logger = Logger.getLogger(GUI.class);
    
    public GUI() {
        final SmsSenderFrame smsSenderFrame = new SmsSenderFrame();
        smsSenderFrame.setVisible(true);
   
        try {
            logger.info("Loading favorite settings...");
            Properties favorite = new Properties();
            favorite.load(getClass().getResourceAsStream("/favorite.properties"));
            smsSenderFrame.setSender(favorite.getProperty("sender"));
            smsSenderFrame.setPrefix(favorite.getProperty("prefix"));
            smsSenderFrame.setNumber(favorite.getProperty("phone_number"));
            logger.info("Favorite settings were loaded.");
        } catch (IOException e) {
            logger.error("Favorite settings were not loaded!", e);
        }

        final AboutDialog aboutDialog = new AboutDialog(smsSenderFrame);
        smsSenderFrame.getAboutMenuItem().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                aboutDialog.setVisible(true);
            }
        });
        
        final ContactList contactList = new ContactList(); 
        contactList.load();
        final ContactsDialog contactsDialog = new ContactsDialog(smsSenderFrame, contactList);
        smsSenderFrame.getContactListMenuItem().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                contactsDialog.setVisible(true);
            }
        });
        
        smsSenderFrame.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent e) {
                contactList.save();
            }
        });
        
        final ContactNameDialog contactNameDialog = new ContactNameDialog(smsSenderFrame, contactsDialog);
        smsSenderFrame.getSaveContactMenuItem().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                contactNameDialog.setVisible(true);
            }
        });
    }

    /**
     * Launches this application
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUI();
            }
        });
    }

}
